package com.lanwf.admin.sys.index;

import com.jfinal.plugin.activerecord.Db;
import com.lanwf.common.model.SysAdmin;
import com.lanwf.common.model.SysMenu;
import com.lanwf.common.model.SysPermission;
import com.lanwf.admin.utils.CommonUtil;
import com.lanwf.admin.vo.MenuTows;
import com.lanwf.admin.vo.Menus;

import java.util.*;

/**
 * 后台首页业务层
 */
public class IndexAdminService {
	

	/**
	 * 获取账户总数
	 */
	public int getTotalAccount() {
		return Db.queryInt("select count(*) from lg_account");
	}



	public List<Menus> getListMenus(SysAdmin account){
        Object listMenus = CommonUtil.getCache("listMenus");
        if(listMenus != null){
            return (ArrayList<Menus>)listMenus;
        }

        List<Menus> menus = null;
        //规定id1管理员
        if(account.getId().intValue() == 1){
            menus = listMenusAdmin(account);
        }else{
            menus = setListMenus(account);
        }

        CommonUtil.setCache("listMenus",menus);
        return menus;
    }

	/**
	 * 菜单列表
	 * @param account
	 * @return
	 */
	public List<Menus> setListMenus(SysAdmin account){

        List<SysPermission> permissions = SysPermission.dao.find("select * from sys_permission where account_id = ? ", account.getId());
		List<SysMenu> sysMenus = SysMenu.dao.findAll();
		HashMap<String, SysMenu> setData = new HashMap<>();
		for (SysMenu sysMenu : sysMenus) {
			setData.put(sysMenu.getId().toString(), sysMenu);
		}

		HashMap<String, Menus> map = new HashMap<>();
		for (SysPermission permission : permissions) {

			map.get(permission.getPid());

			if(map.get(permission.getPid().toString()) == null){
				ArrayList<MenuTows> menuTows = new ArrayList<>();
				SysMenu o = setData.get(permission.getPid().toString());
				Menus menus = new Menus();
				menus.setIcon(o.getIcon());
				menus.setId(o.getId());
				menus.setLink(o.getLink());
				menus.setName(o.getName());

				SysMenu o1 = setData.get(permission.getMenuId().toString());

				MenuTows m = new MenuTows();
				m.setId(o1.getId());
				m.setName(o1.getName());
				m.setIcon(o1.getIcon());
				m.setLink(o1.getLink());
				menuTows.add(m);

				menus.setMenusList(menuTows);

				map.put(permission.getPid().toString(),menus);
			}else{

				Menus menus = map.get(permission.getPid().toString());
				List<MenuTows> menusList = menus.getMenusList();
				SysMenu o1 = setData.get(permission.getMenuId().toString());
				MenuTows m = new MenuTows();
				m.setId(o1.getId());
				m.setName(o1.getName());
				m.setIcon(o1.getIcon());
				m.setLink(o1.getLink());

				if(menusList == null){
					menusList = new ArrayList<>();
				}
				menusList.add(m);
			}

		}

		List<Menus> menusList = new ArrayList<>();

		for (Map.Entry<String, Menus> entry : map.entrySet()) {
			menusList.add(entry.getValue());
		}
		return menusList;
	}

    public List<Menus> listMenusAdmin(SysAdmin account){

        List<SysMenu> psSysMenus = SysMenu.dao.find("select * from sys_menu where pid > 0 order by sort desc,create_time asc");

        List<SysMenu> all = SysMenu.dao.find("select * from sys_menu order by sort desc,create_time asc ");
        HashMap<String, SysMenu> setData = new HashMap<>();
        for (SysMenu sysMenu : all) {
            setData.put(sysMenu.getId().toString(), sysMenu);
        }

        HashMap<String, Menus> map = new HashMap<>();
        for (SysMenu o: psSysMenus) {

            if(map.get(o.getPid().toString()) == null){
                SysMenu obj = setData.get(o.getPid().toString());
                ArrayList<MenuTows> menuTows = new ArrayList<>();
                Menus menus = new Menus();
                menus.setIcon(obj.getIcon());
                menus.setId(obj.getId());
                menus.setLink(obj.getLink());
                menus.setName(obj.getName());
                menus.setSort(obj.getSort());

                SysMenu o1 = setData.get(o.getId().toString());

                MenuTows m = new MenuTows();
                m.setId(o1.getId());
                m.setName(o1.getName());
                m.setIcon(o1.getIcon());
                m.setLink(o1.getLink());
                m.setSort(o1.getSort());

                menuTows.add(m);

                menus.setMenusList(menuTows);

                map.put(o.getPid().toString(),menus);
            }else{

                Menus menus = map.get(o.getPid().toString());
                List<MenuTows> menusList = menus.getMenusList();
                SysMenu o1 = setData.get(o.getId().toString());
                MenuTows m = new MenuTows();
                m.setId(o1.getId());
                m.setName(o1.getName());
                m.setIcon(o1.getIcon());
                m.setLink(o1.getLink());
                m.setSort(o1.getSort());

                if(menusList == null){
                    menusList = new ArrayList<>();
                }
                menusList.add(m);
            }
        }

        List<Menus> menusList = new ArrayList<>();

        for (Map.Entry<String, Menus> entry : map.entrySet()) {
            menusList.add(entry.getValue());
        }

        for (Menus menus : menusList) {
            Collections.sort(menus.getMenusList(), (o1, o2) -> {
                if(o2.getSort() > o1.getSort()){
                    return -1;
                }
                return 0;
            });
        }

        Collections.sort(menusList, (o1, o2) -> {
            if(o2.getSort() > o1.getSort()){
                return -1;
            }
            return 0;
        });

        return menusList;
    }
}



